﻿import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import PageResult from '@/store/entities/page-result';
import Ajax from '@/lib/ajax';
import FormScheme from '@/store/entities/form/form-scheme/form-scheme';

interface FormSchemeState extends ListState<FormScheme>{
    formSchemeForEdit:any,
    excelFile:any,
    formScheme:any,
}

class FormSchemeModule extends ListModule<FormSchemeState,any,FormScheme>{
    state={
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<FormScheme>(),
        loading: false,
        formSchemeForEdit:{},
        excelFile:{},
        formScheme:{},
    };
    actions={
        async obtainFormSchemes(context:ActionContext<FormSchemeState, any>,payload:any){
            context.state.loading=true;
            const response=await Ajax.post('/api/services/app/FormScheme/ObtainFormSchemes',payload.data);
            context.state.loading=false;
            const result=response.data.result as PageResult<FormScheme>;
            context.state.totalCount=result.totalCount;
            context.state.list=result.items;
        },
        async createOrUpdateFormScheme(context:ActionContext<FormSchemeState, any>,payload:any){
            await Ajax.post('/api/services/app/FormScheme/CreateOrUpdateFormScheme',payload.data);
        },
        async deleteFormScheme(context:ActionContext<FormSchemeState, any>,payload:any){
            await Ajax.delete('/api/services/app/FormScheme/DeleteFormScheme?Id='+payload.data.id);
        },
        async getFormSchemeForEdit(context:ActionContext<FormSchemeState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FormScheme/GetFormSchemeForEdit?Id='+payload.data.id);
            context.state.formSchemeForEdit=response.data.result;
        },
        async getFormScheme(context:ActionContext<FormSchemeState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FormScheme/GetFormScheme?Id='+payload.data.id);
            context.state.formScheme=response.data.result;
        },
        async obtainFormSchemesToExcel(context:ActionContext<FormSchemeState, any>,payload:any){
            const response=await Ajax.post('/api/services/app/FormScheme/ObtainFormSchemesToExcel',payload.data);
            context.state.excelFile=response.data.result;
        },
    };
    mutations = {
        setCurrentPage(state: FormSchemeState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: FormSchemeState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}
const formSchemeModule=new FormSchemeModule();
export default formSchemeModule;
